﻿using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class CarScrapService
    {
        private readonly CarScrapDAO dal = new CarScrapDAO();

        #region  BasicMethod
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CarScrap model, IList<Sys_Attachments> list)
        {
            var tag = 0;
            if (model.Id == 0)
            {
                tag = dal.Add(model);
            }
            else
            {
                tag = model.Id;
                dal.Update(model);
            }
            if (tag > 0 && list.Count > 0)
                new Sys_AttachmentsDAO().SaveAttachments(list, tag);
            return tag;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CarScrap model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }
        /// <summary>
        /// 根据条件获取前几条数据
        /// </summary>
        /// <param name="strWhere">The Query String.</param>
        /// <returns>RepairManual List</returns>
        public IList<CarScrap> GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 根据ID获取明显
        /// </summary>
        /// <param name="id">主信息ID</param>
        /// <returns></returns>
        public CarScrap GetModel(int id)
        {
            string where = " AND Id=" + id;
            var ent = GetList(where).FirstOrDefault();
            ent.AttactList = new Sys_AttachmentsDAO().GetList("CSRP", id);
            return ent;
        }
        #endregion  BasicMethod
        #region UserMethod
        /// <summary>
        /// 获取报废列表
        /// </summary>
        /// <param name="orgz">机构ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="start">用车开始时间</param>
        /// <param name="end">用车结束时间</param>
        /// <param name="carNo">车牌号</param>
        /// <returns></returns>
        public CommModel<CarScrap> GetList(int orgz, int pageSize, int pageIndex, string start, string end, string carNo)
        {
            string where = string.Empty;
            if (orgz > 0)
            {
                where = "AND OrgzNo=" + orgz;
            }
            if (!string.IsNullOrEmpty(start))
            {
                where += " AND ScrapTime>='" + start + "' AND ScrapTime<='" + end + "'";
            }
            if (!string.IsNullOrEmpty(carNo))
            {
                where += " AND CarNo Like '%" + carNo + "'";
            }
            Hashtable param = new Hashtable();
            param.Add("A1", pageIndex);
            param.Add("A2", pageSize);
            param.Add("A3", where);
            param.Add("A4", "Vw_CarScrap");
            param.Add("A5", "CreateTime DESC");
            var ht = CommDAO.CommPagination(param);
            DataTable dt = ht["data"] as DataTable;
            List<CarScrap> list = new List<CarScrap>();
            foreach (DataRow item in dt.Rows)
            {
                list.Add(DataRowToModel(item));
            }
            CommModel<CarScrap> ent = new CommModel<CarScrap>();
            ent.Data = list;
            ent.PageCount = Convert.ToInt32(ht["PageCount"]);
            ent.RecordCount = Convert.ToInt32(ht["record"]);
            return ent;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CarScrap DataRowToModel(DataRow row)
        {
            CarScrap model = new CarScrap();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["ScrapAddress"] != null)
                {
                    model.ScrapAddress = row["ScrapAddress"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.OrgzName = row["Name"].ToString();
                }
                if (row["RegistrationTime"] != null && row["RegistrationTime"].ToString() != "")
                {
                    model.RegistrationTime = DateTime.Parse(row["RegistrationTime"].ToString());
                }
                if (row["ScrapTime"] != null && row["ScrapTime"].ToString() != "")
                {
                    model.ScrapTime = DateTime.Parse(row["ScrapTime"].ToString());
                }
                if (row["ResidualAmount"] != null && row["ResidualAmount"].ToString() != "")
                {
                    model.ResidualAmount =  Convert.ToDecimal(row["ResidualAmount"].ToString());
                }
                if (row["ResidualValue"] != null && row["ResidualValue"].ToString() != "")
                {
                    model.ResidualValue = Convert.ToDecimal(row["ResidualValue"].ToString());
                }
                if (row["ScrapReson"] != null && row["ScrapReson"].ToString() != "")
                {
                    model.ScrapReson = row["ScrapReson"].ToString();
                }
                if (row["ServiceLife"] != null && row["ServiceLife"].ToString() != "")
                {
                    model.ServiceLife = int.Parse(row["ServiceLife"].ToString());
                }
                if (row["CarInfoId"] != null && row["CarInfoId"].ToString() != "")
                {
                    model.CarNo = row["CarNo"].ToString();
                    model.CarInfoId = Convert.ToInt32(row["CarInfoId"]);
                }
            }
            return model;
        }
        #endregion
    }
}
